﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Youtube_HD.Model
{
    public class DetectScrollState
    {
        private ScrollViewer _scrollViewer;
        public delegate void ScrollEvent();
        public event ScrollEvent ScrollingDownToEnd;
        public event ScrollEvent ScrollingRightToEnd;
        public DetectScrollState(object targetControl, bool isPhone)
        {
            if (targetControl != null)
            {
                if (targetControl is ListView)
                {
                    _scrollViewer = (ScrollViewer)FindElementRecursive((ListView)targetControl, typeof(ScrollViewer));
                }
                else
                {
                    _scrollViewer = (ScrollViewer)FindElementRecursive((GridView)targetControl, typeof(ScrollViewer));
                }
                if (_scrollViewer != null)
                {

                    _scrollViewer.ViewChanging += _scrollViewerOnPhone_ViewChanging;
                    _scrollViewer.LayoutUpdated += _scrollViewerOnPhone_LayoutUpdated;
                    _scrollViewer.Unloaded += _scrollViewer_Unloaded;
                }
            }
        }

        private void _scrollViewerOnPhone_ViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
        {

            bool isScrollDown = e.FinalView.VerticalOffset - e.NextView.VerticalOffset > 0 ? true : false;
            if (_scrollViewer.ScrollableHeight - _scrollViewer.VerticalOffset < 800 &&
                isScrollDown)
            {
                ScrollingDownToEnd();
            }
        }

        private void _scrollViewerOnPhone_LayoutUpdated(object sender, object e)
        {
            _scrollViewer.LayoutUpdated -= _scrollViewerOnPhone_LayoutUpdated;
            if ((_scrollViewer.ScrollableHeight != 0) &&
                (_scrollViewer.ScrollableHeight == _scrollViewer.VerticalOffset))
            {
                ScrollingDownToEnd();
            }
        }

        private void _scrollViewerOnTablet_ViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
        {
            bool isScrollRight = e.FinalView.HorizontalOffset - e.NextView.HorizontalOffset > 0 ? true : false;
            if (_scrollViewer.ScrollableWidth - _scrollViewer.HorizontalOffset < 800 &&
                isScrollRight)
            {
                ScrollingRightToEnd();
            }
        }

        private void _scrollViewerOnTablet_LayoutUpdated(object sender, object e)
        {
            if ((_scrollViewer.ScrollableWidth != 0) &&
               (_scrollViewer.ScrollableWidth == _scrollViewer.HorizontalOffset))
            {
                ScrollingRightToEnd();
            }
        }

        private void _scrollViewer_Unloaded(object sender, RoutedEventArgs e)
        {
            _scrollViewer.LayoutUpdated -= _scrollViewerOnPhone_LayoutUpdated;
            _scrollViewer.LayoutUpdated -= _scrollViewerOnTablet_LayoutUpdated;
        }

        private UIElement FindElementRecursive(FrameworkElement parent, Type targetType)
        {
            int childCount = VisualTreeHelper.GetChildrenCount(parent);
            UIElement returnElement = null;
            if (childCount > 0)
            {
                for (int i = 0; i < childCount; i++)
                {
                    Object element = VisualTreeHelper.GetChild(parent, i);
                    if (element.GetType() == targetType)
                    {
                        return element as UIElement;
                    }
                    else
                    {
                        returnElement = FindElementRecursive(VisualTreeHelper.GetChild(parent, i) as FrameworkElement, targetType);
                    }
                }
            }
            return returnElement;
        }
    }
}
